﻿using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YuFeng.Models;
using YuFeng.Repositories;
using YuFeng.Repositories.News;

namespace YuFeng.Services.News
{
    public interface INewsService : IBaseService<Models.New.News>
    {
        IPagedList<Models.New.News> GetPageNews(string title, bool? isdelete, int page = 1, int size = 10);
        Models.New.News GetNewsByID(string id);
        IPagedList<Models.New.News> GetHonors(string title, bool? isdelete, int page = 1, int size = 10);
        Models.New.News GetIntroduce();
        Models.New.News GetContactUs();
    }
    public class NewsService : BaseService<Models.New.News>, INewsService
    {
        private readonly INewsRepository _newsRepository;
        private readonly IUnitOfWork _unitOfWork;
        public NewsService(INewsRepository newsRepository, IUnitOfWork unitOfWork)
            : base(newsRepository, unitOfWork)
        {
            this._newsRepository = newsRepository;
            this._unitOfWork = unitOfWork;
        }
        public IPagedList<Models.New.News> GetPageNews(string title, bool? isdelete, int page = 1, int size = 10)
        {
            var query = _newsRepository.GetAll().Where(o => o.Type == (int)YuFeng.Models.AppEnum.NewsType.CompanyNews || o.Type == (int)YuFeng.Models.AppEnum.NewsType.InlineNews);

            if (!string.IsNullOrWhiteSpace(title))
            {
                query = query.Where(e => e.Title.IndexOf(title) > -1);
            }
            if (isdelete.HasValue)
            {
                query = query.Where(e => e.IsDeleted == isdelete.Value);
            }

            return query.GetPage(o => o.CreatedTime, page, size, true);
        }
        public Models.New.News GetNewsByID(string id)
        {
            var query = _newsRepository.GetById(id);
            return query;
        }
        public IPagedList<Models.New.News> GetHonors(string title, bool? isdelete, int page = 1, int size = 10)
        {
            var query = _newsRepository.GetAll().Where(o => o.Type == (int)AppEnum.NewsType.PatentCertificate || o.Type == (int)AppEnum.NewsType.HonorCertificate);

            if (!string.IsNullOrWhiteSpace(title))
            {
                query = query.Where(e => e.Title.IndexOf(title) > -1);
            }
            if (isdelete.HasValue)
            {
                query = query.Where(e => e.IsDeleted == isdelete.Value);
            }

            return query.GetPage(o => o.CreatedTime, page, size, true);
        }
        public Models.New.News GetIntroduce()
        {
            var query = _newsRepository.Get(o => o.Type == (int)AppEnum.NewsType.Introduce);
            return query;
        }
        public Models.New.News GetContactUs()
        {
            var query = _newsRepository.Get(o => o.Type == (int)AppEnum.NewsType.ContactUs);
            return query;
        }
    }
}
